<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/2/27
  Time: 19:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>音频管理页面</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js" type="text/javascript"></script>
</head>
<body>
    <!--展示表格-->
    <table id="myTable" lay-filter="tableFilter"></table>
    <!--表格显示图片-->
    <script type="text/html" id="showImg">
        <img src="${pageContext.request.contextPath}/album/{{d.audioUrl}}" height="50px" width="100px"/>
    </script>
    <!--toolbar组件-->
    <script type="text/html" id="bar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" lay-event="delete">删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
        </div>
    </script>
    <!--修改表单-->
    <form class="layui-form" id="updateForm" lay-filter="updateFormFilter" style="display: none;padding: 15px 15px 5px 5px"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">音频名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="audioId"/>
                <input type="hidden" name="audioUrl"/>
                <input type="text" name="audioName" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">音频封面图</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="updateAudioImg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属专辑</label>
            <div class="layui-input-block">
                <select name="albumId" id="album2" lay-filter="albumFilter1">
                    <option>--选择专辑--</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">音频大小</label>
            <div class="layui-inline">
                <input type="text" name="audioSize" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">排序字段</label>
            <div class="layui-inline">
                <input type="text" name="audioOrder" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updateFilter">修改</button>
            </div>
        </div>
    </form>

<!--js代码-->
    <script>
        layui.use(['table','form','laydate','upload','layer'],function () {
            var table = layui.table;
            var form = layui.form;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var layer = layui.layer;
            var $ = layui.$;
            //0、获取上个页面传递过来的参数 albumId
            var url = window.location.href;
            //因为只有一个参数 所以截取？后面的那个元素即是传递的参数album=value
            var parm = url.substring(url.lastIndexOf('?')+1);
            // var albumId = parm.split('=')[1];//将参数album=value拆分  得到album的值
            // console.log('parm='+parm);
            // console.log('albumId='+albumId);
            //1、渲染表格
            var tableIns = table.render({
                elem:'#myTable',
                url:'${pageContext.request.contextPath}/cmfzAudio/show?'+parm,
                page:true,
                limit:3,
                limits:[3,4,5],
                cols:[[
                    {field:'audioId',title:'编号',sort:true,width:120},
                    {field:'audioName',title:'音频名称',sort:true,width:120},
                    {field:'audioUrl',title:'封面图',templet:'#showImg'},
                    {field:'albumId',title:'所属专辑编号',sort:true,width:120},
                    {field:'audioSize',title:'大小',sort:true,width:120},
                    {field:'audioOrder',title:'排序',sort:true,width:120},
                    {title:'操作',toolbar:'#bar'}
                ]]
            })
            //2.监听toolbar工具栏组件  执行删除和修改操作
            table.on('tool(tableFilter)',function (obj) {
                if(obj.event=='delete'){
                    layer.confirm('确认要删除该项吗？',function (index) {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzAudio/remove',
                            data:'audioId='+obj.data.audioId,
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('myTable');
                                }else {
                                    layer.msg('删除失败');
                                }
                            }
                        })

                        layer.close(index);
                    })
                }else if(obj.event=='update'){
                    //执行修改操作
                    layer.open({
                        type:1,
                        title:'修改页面',
                        area:'900px',
                        content:$('#updateForm')
                    })
                    //加载所属专辑下拉框
                    $.ajax({
                        url:'${pageContext.request.contextPath}/cmfzAlbum/selectAllCmfzAlbum',
                        success:function (result) {
                            var s = '';
                            //遍历查询出的专辑集合  将每一个专辑逐次添加下拉框内 记得刷新下拉框
                            $(result).each(function (i, obj) {
                                s += '<option value="'+obj.albumId+'">'+obj.albumName+'</option>';
                                $('#album2').html(s);
                                //刷新下拉框  因为下拉框数据是动态更新的
                                form.render('select');
                            })
                        }
                    })
                    //回显
                    $.ajax({
                        url:'${pageContext.request.contextPath}/cmfzAudio/getOne',
                        data:'audioId='+obj.data.audioId,
                        success:function (result) {
                            form.val('updateFormFilter',{
                                audioId:result.audioId,
                                audioName:result.audioName,
                                albumId:result.albumId,
                                audioUrl:result.audioUrl,
                                audioSize:result.audioSize,
                                audioOrder:result.audioOrder
                            })
                        }
                    })
                    //加载图片上传组件
                    upload.render({
                        elem:'#updateAudioImg',
                        url:'${pageContext.request.contextPath}/cmfzAlbum/upload',
                        done:function (result) {
                            if(result.flag){
                                layer.msg('图片上传成功');
                                form.val('updateFormFilter',{
                                    audioUrl:result.fileName
                                })
                            }else{
                                layer.msg('图片上传失败');
                            }
                        }
                    })
                    //提交更新表单
                    form.on('submit(updateFilter)',function () {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/cmfzAlbum/update',
                            data:$('#updateForm').serialize(),
                            success:function (result) {
                                if(result.flag){
                                    layer.msg('修改成功');
                                    layer.closeAll();
                                    table.reload('myTable');
                                }else {
                                    layer.msg('修改失败');
                                }
                            }
                        })
                        return false;
                    })

                }
            })


        })
    </script>
</body>
</html>
